home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Prefs / New / MPPalette.cold < prev    next >
Text File  |  1998-08-08  |  48KB  |  1,552 lines

  1. #include "mpp.h"
  2. #include <proto/gadtools.h>
  3. #include <proto/graphics.h>
  4. #include <clib/extras_protos.h>
  5. #include <extras/layoutgt.h>
  6. #include <extras/ext_text.h>
  7.  
  8. extern UBYTE    ModePro[];
  9. extern STRPTR   MiscText[];
  10. extern UBYTE    filename[513];
  11. extern struct   FileRequester *PalFileReq;
  12.  
  13. /*
  14. BOOL ReadCMAP(STRPTR Filename, struct RGB *CReg, ULONG Colors);
  15.  
  16.  
  17. #define MAX_PAL_COLORS 256
  18.  
  19. /* Gad IDs */
  20. #define PGD_PALETTE 0
  21. #define PGD_RED     1
  22. #define PGD_GREEN   2
  23. #define PGD_BLUE    3
  24. #define PGD_OK      4
  25. #define PGD_CANCEL  5
  26. #define PGD_RESET   6
  27. #define PGD_UNDO    7
  28. #define PGD_COPY    8
  29. #define PGD_SWAP    9
  30. #define PGD_SPREAD  10
  31.  
  32. /* Project Menu */
  33. #define M_PALETTELOAD       FULLMENUNUM(0,0,NOSUB)
  34. #define M_PALETTESAVE       FULLMENUNUM(0,1,NOSUB)
  35. /* Bar */
  36. #define M_PALETTEOK         FULLMENUNUM(0,3,NOSUB)
  37. #define M_PALETTECANCEL     FULLMENUNUM(0,4,NOSUB)
  38.  
  39. BOOL SelectPaletteFile(UBYTE *title,UBYTE save,struct Window *Parent);
  40.  
  41. struct   NewMenu PaletteMenu[]=
  42. {
  43.   {NM_TITLE ,(STRPTR)MSG_M_PROJECT              ,0  ,0,0,0},
  44.   {NM_ITEM  ,(STRPTR)MSG_PRM_LOADPALETTE        ,(STRPTR)MSG_PRM_LOADPALETTE_KEY        ,0,0,(APTR)M_PALETTELOAD},
  45.   {NM_ITEM  ,(STRPTR)MSG_PRM_SAVEPALETTE        ,(STRPTR)MSG_PRM_SAVEPALETTE_KEY        ,0,0,(APTR)M_PALETTESAVE},
  46.   {NM_ITEM  ,NM_BARLABEL                ,0  ,0,0,0},
  47.   {NM_ITEM  ,(STRPTR)MSG_PRM_OK                 ,(STRPTR)MSG_PRM_OK_KEY                 ,0,0,(APTR)M_PALETTEOK},
  48.   {NM_ITEM  ,(STRPTR)MSG_PRM_CANCEL             ,(STRPTR)MSG_PRM_CANCEL_KEY             ,0,0,(APTR)M_PALETTECANCEL},
  49.   {NM_END   ,NULL                       ,0 ,0,0,0}
  50. };
  51.  
  52. //extern struct Hook PaletteWinHook;
  53.  
  54. /*
  55. struct LetterGad PReqLetters[]=
  56. {
  57.   (STRPTR)MSG_PRG_PALETTE_KEY ,PGD_PALETTE,
  58.   (STRPTR)MSG_PRG_RED_KEY     ,PGD_RED,
  59.   (STRPTR)MSG_PRG_GREEN_KEY   ,PGD_GREEN,
  60.   (STRPTR)MSG_PRG_BLUE_KEY    ,PGD_BLUE,
  61.   (STRPTR)MSG_PRG_RESET_KEY   ,PGD_RESET,
  62.   (STRPTR)MSG_PRG_UNDO_KEY    ,PGD_UNDO,
  63.   (STRPTR)MSG_PRG_COPY_KEY    ,PGD_COPY,
  64.   (STRPTR)MSG_PRG_SWAP_KEY    ,PGD_SWAP,
  65.   (STRPTR)MSG_PRG_SPREAD_KEY  ,PGD_SPREAD,
  66.   (STRPTR)MSG_PRG_OK_KEY      ,PGD_OK,
  67.   (STRPTR)MSG_PRG_CANCEL_KEY  ,PGD_CANCEL,
  68.   (STRPTR)~0,~0
  69. };
  70. */
  71.  
  72. extern struct Screen *Scr;
  73. extern struct Window *Win;
  74. extern struct TextAttr *TAttr;
  75. extern APTR VI;
  76. extern struct TextFont *GUIFont;
  77. extern BOOL V39;
  78.  
  79. struct PaletteReq
  80. {
  81.   struct Window *pr_Window;
  82.   struct LG_Control *pr_GadControl;
  83.   struct DefaultNode *pr_DefaultNode;
  84.   ULONG pr_RetVal,
  85.         pr_Mode,
  86.         pr_ModeColor;
  87.   LONG  pr_RedLevel,
  88.         pr_GreenLevel,
  89.         pr_BlueLevel,
  90.         pr_RedMax,
  91.         pr_GreenMax,
  92.         pr_BlueMax;
  93.   UBYTE pr_RedBits,
  94.         pr_GreenBits,
  95.         pr_BlueBits;
  96.   WORD  pr_RedMult,
  97.         pr_GreenMult,
  98.         pr_BlueMult;
  99.   UWORD pr_Colors;
  100.   UWORD pr_Offset;
  101.   struct RGB pr_Palette[MAX_PAL_COLORS];
  102.   ULONG pr_End;
  103.   ULONG pr_ActiveColor,
  104.         pr_PrevColor;
  105.   struct RGB pr_UndoBuffer[MAX_PAL_COLORS];
  106.   struct Menu *pr_MenuStrip;
  107.   BOOL  pr_TrueColor;
  108.   APTR  pr_VI;
  109.   WORD  pr_PLeft,pr_PTop,pr_PWidth,pr_PHeight;
  110.   LONG  pr_PenMap[MAX_PAL_COLORS];
  111.   //UWORD pr_PenList[MAX_PAL_COLORS];
  112. };
  113.  
  114. #define PRM_NORMAL 0
  115. #define PRM_COPY   1
  116. #define PRM_SWAP   2
  117. #define PRM_SPREAD 3
  118.  
  119. struct PaletteReq PReq={0};
  120.  
  121. void ProcessPalGads(struct IntuiMessage *IMsg);
  122. void ProcessPalKeys(struct IntuiMessage *Imsg);
  123. LONG LongestTextLen(struct RastPort *RP,STRPTR String, ... );
  124. struct LG_Control *LayoutPaletteGads(struct Window *PWin, APTR PVI);
  125. void SetPalette(void);
  126. void UpdateSliders(void);
  127. void GetPalette(void);
  128. void SetUndoBuffer(void);
  129. void GetUndoBuffer(void);
  130. ULONG Mix(ULONG A,ULONG B, float Percent);
  131. void ProcessPalMenus(struct DefaultNode *DN, struct Menu *MenuStrip, struct IntuiMessage *imsg);
  132.  
  133. void PGD_Cancel(void);
  134. void PGD_Ok(void);
  135. void PGD_Palette(LONG Active, BOOL Keyed);
  136. void PGD_PaletteModed(LONG Active, BOOL Keyed);
  137. void PGD_Red(LONG Value, BOOL Keyed);
  138. void PGD_Green(LONG Value, BOOL Keyed);
  139. void PGD_Blue(LONG Value, BOOL Keyed);
  140. void PGD_Undo(void);
  141. void PGD_Reset(void);
  142. BOOL PGD_Copy(void);
  143. BOOL PGD_Swap(void);
  144. BOOL PGD_Spread(void);
  145.  
  146. void DrawPenBox(void);
  147. void DrawPenRect(void);
  148.  
  149.  
  150. UBYTE PenArray[MAX_PAL_COLORS];
  151. LONG PenCount;
  152.  
  153. LONG LongestTextLen(struct RastPort *RP,STRPTR String, ... )
  154. {
  155.   STRPTR *str;
  156.   LONG len=0,n;
  157.   
  158.   str=&String;
  159.   
  160.   while(*str)
  161.   {
  162.     n=StrLength(SL_TextFont     ,RP->Font,
  163.                 SL_String       ,*str,
  164.                 SL_IgnoreChars  , "_",
  165.                 TAG_DONE);
  166.     len=max(n,len);
  167.     str++;
  168.   }
  169.   
  170.   return(len);
  171. }
  172.  
  173. WORD MinWidth,MinHeight;
  174.  
  175. struct LG_Control *LayoutPaletteGads(struct Window *PWin, APTR PVI)
  176. {
  177.   WORD width,height,
  178.        sliderlabellen,slidertopedge,slidernumlen,
  179.        buttontopedge,buttonheight,buttonwidth,
  180.        undobuttontopedge, undobuttonwidth,
  181.        editbuttontopedge, editbuttonwidth;
  182.   struct RastPort *rp;       
  183.   struct LG_Control *pr_control;
  184.   STRPTR ok,cancel,red,green,blue,reset,undo,copy,swap,spread;
  185.  
  186.   if(PVI && PWin)
  187.   {
  188.     rp=PWin->RPort; 
  189.     
  190.     width =PWin->Width  - PWin->BorderLeft- PWin->BorderRight  - 8;
  191.     height=PWin->Height - PWin->BorderTop - PWin->BorderBottom - 8 - 20;
  192.     
  193.     PReq.pr_PLeft = PWin->BorderLeft + 4;
  194.     PReq.pr_PTop  = PWin->BorderTop  + 4;
  195.     PReq.pr_PWidth= width;
  196.     PReq.pr_PHeight= 16;
  197.     
  198.     
  199.     slidernumlen=MaxStrFontLen(GUIFont,4,'0','9');
  200.     
  201.     buttonheight=TAttr->ta_YSize+6;
  202.     
  203.     buttontopedge=height-buttonheight;
  204.     slidertopedge=buttontopedge-6-(buttonheight*3);
  205.     undobuttontopedge=slidertopedge-buttonheight-3;
  206.     editbuttontopedge=undobuttontopedge-buttonheight;
  207.   
  208.     /* init strings */
  209.     ok      =GetString(MSG_PRG_OK);
  210.     cancel  =GetString(MSG_PRG_CANCEL);
  211.     red     =GetString(MSG_PRG_RED);
  212.     green   =GetString(MSG_PRG_GREEN);
  213.     blue    =GetString(MSG_PRG_BLUE);
  214.     reset   =GetString(MSG_PRG_RESET);
  215.     undo    =GetString(MSG_PRG_UNDO);
  216.     copy    =GetString(MSG_PRG_COPY);
  217.     swap    =GetString(MSG_PRG_SWAP);
  218.     spread  =GetString(MSG_PRG_SPREAD);
  219.     
  220.     /* init lens */
  221.     buttonwidth     =LongestTextLen(rp,ok,cancel,0)+8;
  222.     sliderlabellen  =LongestTextLen(rp,red,green,blue,0)+8;
  223.     undobuttonwidth =LongestTextLen(rp,reset,undo,0)+8;
  224.     editbuttonwidth =LongestTextLen(rp,copy,swap,spread,0)+8;
  225.   
  226.     MinHeight=buttonheight*6+6+60+PWin->BorderLeft+PWin->BorderRight+20;
  227.     MinWidth =sliderlabellen+slidernumlen+40;
  228.     MinWidth =max(MinWidth,buttonwidth*2);
  229.     MinWidth =max(MinWidth,undobuttonwidth*2);
  230.     MinWidth =max(MinWidth,editbuttonwidth*3)+PWin->BorderTop+PWin->BorderBottom;
  231.   
  232.     undobuttonwidth =max(width/2,undobuttonwidth);
  233.  
  234.   
  235.     pr_control=LG_CreateGadgets( /* init shit */
  236.                                  LG_VisualInfo            ,PVI,
  237.                                  LG_UseWindowOffsets      ,PWin,
  238.                                  LG_EraseRemoved  ,FALSE,
  239.                                  LG_BoundsLeft    ,4,
  240.                                  LG_BoundsTop     ,4 + 20,
  241.                                  LG_BoundsWidth   ,width,
  242.                                  LG_BoundsHeight  ,height,
  243.                                  LG_TextAttr      ,TAttr,
  244.                                  
  245.                                  /* Palette Gadget */
  246.                                  LG_GadgetKind    ,PALETTE_KIND,
  247.                                  LG_GadgetID      ,PGD_PALETTE,
  248.                                  LG_LeftEdge      ,0,
  249.                                  LG_TopEdge       ,0,
  250.                                  LG_Width         ,LG_REL_WIDTH(0),
  251.                                  LG_Height        ,editbuttontopedge-4,
  252.                                  LG_Flags         ,PLACETEXT_ABOVE,
  253.                                  LG_LabelFlags    ,LGLF_FITLABEL,
  254.                                  LG_GadgetText    ,GetString(MSG_PRG_PALETTE),
  255.                                  LG_GadgetTags    ,4,
  256.                                    GT_Underscore    ,'_',
  257.                                    GTPA_Depth       ,PWin->WScreen->RastPort.BitMap->Depth,
  258.                                    GTPA_Color       ,PReq.pr_ActiveColor,
  259.                                    GTPA_IndicatorWidth,32,
  260.                                  LG_CreateGadget  ,0,
  261.                                  
  262.                                  
  263.                                  /* Slider init */
  264.                                  LG_LeftEdge      ,sliderlabellen,
  265.                                  LG_Width         ,width-sliderlabellen-slidernumlen-8,
  266.                                  LG_Height        ,buttonheight-2,
  267.                                  LG_LabelFlags    ,0,
  268.                                  LG_Flags         ,0,
  269.                                  
  270.                                  /* Red Slider */
  271.                                  LG_GadgetKind    ,SLIDER_KIND,
  272.                                  LG_GadgetID      ,PGD_RED,
  273.                                  LG_TopEdge       ,slidertopedge,
  274.                                  LG_GadgetText    ,red,
  275.                                  LG_GadgetTags    ,9,
  276.                                  GTSL_Level       ,PReq.pr_Palette[PReq.pr_ActiveColor].Red>>24 ,
  277.                                  GTSL_MaxPixelLen ,slidernumlen+1,
  278.                                  GTSL_Min         ,0,
  279.                                  GTSL_Max         ,PReq.pr_RedMax,
  280.                                  GTSL_LevelFormat ,"%3ld",
  281.                                  GTSL_MaxLevelLen ,4,
  282.                                  GTSL_LevelPlace  ,PLACETEXT_RIGHT,
  283.                                  GTSL_Justification ,GTJ_RIGHT,
  284.                                  GT_Underscore    ,'_',
  285.                                  LG_CreateGadget  ,0,
  286.                                  
  287.                                  /* Green Slider */
  288.                                  LG_TopEdge       ,slidertopedge+buttonheight,
  289.                                  LG_GadgetText    ,green,
  290.                                  LG_GadgetTags    ,9,
  291.                                  GTSL_Level       ,PReq.pr_Palette[PReq.pr_ActiveColor].Green>>24 ,
  292.                                  GTSL_MaxPixelLen ,slidernumlen+1,
  293.                                  GTSL_Min         ,0,
  294.                                  GTSL_Max         ,PReq.pr_GreenMax,
  295.                                  GTSL_LevelFormat ,"%3ld",
  296.                                  GTSL_MaxLevelLen ,4,
  297.                                  GTSL_LevelPlace  ,PLACETEXT_RIGHT,
  298.                                  GTSL_Justification ,GTJ_RIGHT,
  299.                                  GT_Underscore    ,'_',
  300.                                  LG_CreateGadget  ,0,
  301.                                  
  302.                                  /* Blue Slider */
  303.                                  LG_TopEdge       ,slidertopedge+buttonheight*2,
  304.                                  LG_GadgetText    ,blue,
  305.                                  LG_GadgetTags    ,9,
  306.                                  GTSL_Level       ,PReq.pr_Palette[PReq.pr_ActiveColor].Blue>>24 ,
  307.                                  GTSL_MaxPixelLen ,slidernumlen+1,
  308.                                  GTSL_Min         ,0,
  309.                                  GTSL_Max         ,PReq.pr_BlueMax,
  310.                                  GTSL_LevelFormat ,"%3ld",
  311.                                  GTSL_MaxLevelLen ,4,
  312.                                  GTSL_LevelPlace  ,PLACETEXT_RIGHT,
  313.                                  GTSL_Justification ,GTJ_RIGHT,
  314.                                  GT_Underscore    ,'_',
  315.                                  LG_CreateGadget  ,0,
  316.                                  
  317.                                  /* Ok Button */
  318.                                  LG_GadgetTags    ,1,
  319.                                  GT_Underscore    ,'_',
  320.                                  LG_GadgetKind    ,BUTTON_KIND,
  321.                                  LG_GadgetText    ,ok,
  322.                                  LG_LeftEdge      ,0,
  323.                                  LG_TopEdge       ,buttontopedge,
  324.                                  LG_Width         ,buttonwidth,
  325.                                  LG_Height        ,buttonheight,
  326.                                  LG_CreateGadget  ,0,
  327.                                  
  328.                                  /* Cancel Button */
  329.                                  LG_LeftEdge      ,LG_REL_RIGHT(-buttonwidth),
  330.                                  LG_GadgetText    ,cancel,
  331.                                  LG_CreateGadget  ,0,
  332.                                  
  333.                                  
  334.                                  LG_HorizCells    ,2,
  335.                                  /* Reset Button */
  336.                                  LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(0),
  337.                                  LG_TopEdge       ,undobuttontopedge,
  338.                                  LG_Width         ,LG_REL_CELL_WIDTH(1),
  339.                                  LG_Height        ,buttonheight,
  340.                                  LG_GadgetText    ,reset,
  341.                                  LG_CreateGadget  ,0,
  342.                                  
  343.                                  /* Undo button */
  344.                                  LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(1),
  345.                                  LG_GadgetText    ,undo,
  346.                                  LG_CreateGadget  ,0,
  347.                                  
  348.                                  
  349.                                  LG_HorizCells    ,3,
  350.                                  /* Copy */
  351.                                  LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(0),
  352.                                  LG_TopEdge       ,editbuttontopedge,
  353.                                  LG_Width         ,LG_REL_CELL_WIDTH(1),
  354.                                  LG_GadgetText    ,copy,
  355.                                  LG_CreateGadget  ,0,
  356.                                  
  357.                                  /* Swap */
  358.                                  LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(1),
  359.                                  LG_GadgetText    ,swap,
  360.                                  LG_CreateGadget  ,0,
  361.                                  
  362.                                  /* Spread */
  363.                                  LG_LeftEdge      ,LG_REL_CELL_LEFTEDGE(2),
  364.                                  LG_GadgetText    ,spread,
  365.                                  LG_CreateGadget  ,0,
  366.                                  
  367.  
  368.                                  TAG_DONE);
  369.     return(pr_control);
  370.   }
  371.   return(0);
  372. }                              
  373.  
  374. WORD PalWinLeft,PalWinTop,PalWinWidth,PalWinHeight;
  375. BOOL PalFirstTime=TRUE;
  376.  
  377. BOOL PalGo;
  378. /*
  379. void PaletteReq(struct DefaultNode *DN)
  380. {
  381.   struct DisplayInfo di;
  382.   struct DimensionInfo dim;
  383.   struct Requester *req;
  384.   struct Screen *pscr;
  385.   APTR pvi;
  386.   struct IntuiMessage *imsg;
  387.   STRPTR pwintitle,pwinfmt;
  388.   LONG pwinfmtlen;
  389.   WORD dripens[DRIPENS+1];
  390.   WORD swidth=320,sheight=200;
  391.   LONG l;
  392.  
  393.   PalGo=TRUE;
  394.  
  395.   if(PalFirstTime)
  396.   {
  397.     PalWinWidth=   Win->Width/2;
  398.     PalWinHeight=  Win->Height-16;
  399.     PalWinLeft=    Win->LeftEdge+Win->Width/2-PalWinWidth/2;
  400.     PalWinTop=     Win->TopEdge+8;
  401.     PalWinWidth=   max(PalWinWidth, 150);
  402.     PalWinHeight=  max(PalWinHeight,150);
  403.     PalFirstTime=FALSE;
  404.   }
  405.  
  406.   pwinfmt=GetString(MSG_REQ_EDIT_PALETTE);
  407.   pwinfmtlen=strlen(pwinfmt);
  408.   if(pwintitle=AllocVec(pwinfmtlen+strlen(DN->Def_Node.ln_Name)+1,MEMF_PUBLIC|MEMF_CLEAR))
  409.     sprintf(pwintitle,pwinfmt,DN->Def_Node.ln_Name);
  410.   
  411.   /*
  412.   PReq.pr_Colors=1<<DN->Depth;
  413.   PReq.pr_Colors=min(PReq.pr_Colors,MAX_PAL_COLORS);
  414.   PReq.pr_UseScreen=TRUE;
  415.  
  416.   if(V39)
  417.   {
  418.     cm=pscr->ViewPort.ColorMap;
  419.     pen=0;
  420.     for(PenCount=0;PenCount<PReq.pr_Colors && pen!=-1;PenCount++)
  421.     {
  422.       PReq.pr_PenList[PenCount]=pen=ObtainPen(cm,-1,0,0,0,PEN_EXCLUSIVE|PEN_NO_SETCOLOR);
  423.     }
  424.     if(pen==-1)
  425.     {
  426.       for(Pencount--;PenCount>=0;PenCount--)
  427.         ReleasePen(cm,PReq.pr_PenList[PenCount]);
  428.     }
  429.     else
  430.       PReq.pr_UseScreen=FALSE;
  431.   }
  432.  
  433. */
  434.   
  435.   if(DN->Look3D)
  436.   {
  437.     for(l=0;l<DRIPENS;l++)
  438.       dripens[l]=DN->Pens[l];
  439.     dripens[DRIPENS]=~0;
  440.   }
  441.   else
  442.     dripens[0]=~0;
  443.  
  444.   req=Busy(Win);  
  445.  
  446.   if(GetDisplayInfoData(0,(UBYTE *)&dim,sizeof(dim),DTAG_DIMS,DN->ModeID))
  447.   {
  448.     swidth  =max(swidth ,dim.Nominal.MaxX);
  449.     sheight =max(sheight,dim.Nominal.MaxY);
  450.   }
  451.   
  452.   if(pscr=OpenScreenTags(0,SA_Depth     ,DN->Depth,
  453.                            SA_DisplayID ,DN->ModeID,
  454.                            SA_Pens      ,dripens,
  455.                            SA_Overscan  ,OSCAN_TEXT,
  456.                            SA_Width     ,swidth,
  457.                            SA_Height    ,sheight,
  458.                            SA_Title     ,pwintitle,
  459.                            SA_AutoScroll,TRUE,
  460.                            SA_ModeProNode,DN,
  461.                            TAG_DONE))
  462.   {
  463.     /*
  464.     if(drawinfo=GetScreenDrawInfo(pscr))
  465.     {
  466.       //PaletteWinHook.h_Data=drawinfo->dri_Pens[BACKGROUNDPEN];
  467.       FreeScreenDrawInfo(pscr,drawinfo);
  468.     }
  469. */
  470.     PReq.pr_Colors=1<<pscr->RastPort.BitMap->Depth;
  471.     PReq.pr_Colors=min(PReq.pr_Colors,MAX_PAL_COLORS);
  472.     PReq.pr_Palette[PReq.pr_Colors].Red=0;
  473.     PReq.pr_ActiveColor=0;
  474.     PReq.pr_DefaultNode=DN;
  475.     PReq.pr_Mode=0;
  476.   
  477.     if(V39)
  478.       PReq.pr_RedBits   = PReq.pr_GreenBits = PReq.pr_BlueBits = 8;
  479.     else
  480.       PReq.pr_RedBits   = PReq.pr_GreenBits = PReq.pr_BlueBits = 4;
  481.       
  482.     GetPalette();
  483.   
  484.     SetUndoBuffer();
  485.     
  486.     PReq.pr_PrevColor=PReq.pr_ActiveColor;
  487.     
  488.     if(pvi=GetVisualInfo(pscr,0))
  489.     {
  490.       PReq.pr_VI=pvi;
  491.       if(PReq.pr_Window=OpenWindowTags(0,
  492.                         WA_CustomScreen,pscr,
  493.                         WA_Width      ,pscr->Width,
  494.                         WA_Height     ,pscr->Height-pscr->BarHeight-1,
  495.                         WA_Left       ,0,
  496.                         WA_Top        ,pscr->BarHeight+1,
  497.                         WA_Title      ,pwintitle,
  498.                         WA_MaxWidth   ,-1,
  499.                         WA_MaxHeight  ,-1,
  500.                         WA_MinWidth   ,150,
  501.                         WA_MinHeight  ,150,
  502.                         WA_IDCMP      ,PALETTEIDCMP | SLIDERIDCMP | BUTTONIDCMP | 
  503.                                        IDCMP_CLOSEWINDOW|IDCMP_NEWSIZE|IDCMP_VANILLAKEY|IDCMP_MENUPICK|IDCMP_REFRESHWINDOW,
  504.                         WA_SizeGadget ,TRUE,
  505.                         WA_DragBar    ,TRUE,
  506.                         WA_DepthGadget,TRUE,
  507.                         WA_CloseGadget,TRUE,
  508.                         WA_SizeBBottom,TRUE,
  509.                         WA_Activate   ,TRUE,
  510.                         WA_NewLookMenus,TRUE,
  511.                         WA_SimpleRefresh    ,TRUE,
  512. //                        WA_BackFill   ,&PaletteWinHook,
  513.                         TAG_DONE))
  514.       {
  515.         struct Process *proc;
  516.         struct Window *winptr;
  517.     
  518.         proc=(struct Process *)FindTask(0);
  519.         winptr=proc->pr_WindowPtr;
  520.         proc->pr_WindowPtr=PReq.pr_Window;
  521.         
  522.         if(PReq.pr_MenuStrip=CreateMenus(PaletteMenu,TAG_END))
  523.         {
  524.           if(LayoutMenus(PReq.pr_MenuStrip,pvi,
  525.                           GTMN_NewLookMenus,TRUE,
  526.                           TAG_END))
  527.           {
  528.             if(SetMenuStrip(PReq.pr_Window,PReq.pr_MenuStrip))
  529.             {
  530.               SetPalette();
  531.               
  532.               if(GetDisplayInfoData(0,(UBYTE *)&di,sizeof(di),DTAG_DISP,DN->ModeID))
  533.               {
  534.                 if(V39)
  535.                 {
  536.                   PReq.pr_RedBits   =max(di.RedBits,1);
  537.                   PReq.pr_GreenBits =max(di.GreenBits,1);
  538.                   PReq.pr_BlueBits  =max(di.BlueBits,1);
  539.                 }
  540.                 else
  541.                 {
  542.                   /** This needs to be fixed for V36 **/
  543.                   PReq.pr_RedBits   = PReq.pr_GreenBits = PReq.pr_BlueBits = 4;
  544.                   /**********************************************************
  545.                   PReq.pr_RedBits   = sqrt(((di.PaletteRange & 0xf00) >> 8)+1);
  546.                   PReq.pr_GreenBits = sqrt(((di.PaletteRange & 0xf0) >> 4)+1);
  547.                   PReq.pr_BlueBits  = sqrt(((di.PaletteRange & 0xf )  +1));
  548.                   PReq.pr_RedBits   = max(PReq.pr_RedBits      ,1);
  549.                   PReq.pr_GreenBits = max(PReq.pr_GreenBits    ,1);
  550.                   PReq.pr_BlueBits  = max(PReq.pr_BlueBits     ,1);
  551.                   */
  552.                 }
  553.                 
  554.               }
  555.               
  556.               PReq.pr_RedMax   =(1<<PReq.pr_RedBits  )-1;
  557.               PReq.pr_GreenMax =(1<<PReq.pr_GreenBits)-1;
  558.               PReq.pr_BlueMax  =(1<<PReq.pr_BlueBits )-1;
  559.               
  560.               PReq.pr_RedMult   =255/PReq.pr_RedMax;
  561.               PReq.pr_GreenMult =255/PReq.pr_GreenMax;
  562.               PReq.pr_BlueMult  =255/PReq.pr_BlueMax;
  563.               
  564.       
  565.               //printf("%8x %8x\n",PReq.pr_Window->RPort,GUIFont);
  566.               SetFont(PReq.pr_Window->RPort,GUIFont);
  567.               
  568.               //printf("setfont\n");
  569.               
  570.               if(PReq.pr_GadControl=LayoutPaletteGads(PReq.pr_Window,pvi))
  571.               {
  572.                 //printf("Layout\n");
  573.                 DrawPenBox();
  574.                 UpdateSliders();
  575.                 WindowLimits(PReq.pr_Window,MinWidth,MinHeight,-1,-1);
  576.                 LG_AddGadgets(PReq.pr_Window,PReq.pr_GadControl);
  577.                 while(PalGo)
  578.                 {
  579.                   WaitPort(PReq.pr_Window->UserPort);
  580.                   while(imsg=GT_GetIMsg(PReq.pr_Window->UserPort))
  581.                   {
  582.                     switch(imsg->Class)
  583.                     {
  584.                       case IDCMP_REFRESHWINDOW:
  585.                         GT_BeginRefresh(PReq.pr_Window);
  586.                         DrawPenBox();
  587.                         GT_EndRefresh(PReq.pr_Window,TRUE);
  588.                         break;
  589.                         
  590.                       case IDCMP_CLOSEWINDOW:
  591.                         PReq.pr_RetVal=FALSE;
  592.                         PalGo=FALSE;
  593.                         break;
  594.                         
  595.                       case IDCMP_NEWSIZE:
  596.                         LG_FreeGadgets(PReq.pr_GadControl);
  597.                         RefreshWindowFrame(PReq.pr_Window);
  598.                         {
  599.                           WORD x1,y1,x2,y2;
  600.                           
  601.                           x1=PReq.pr_Window->BorderLeft;
  602.                           y1=PReq.pr_Window->BorderTop;
  603.                           x2=PReq.pr_Window->Width  -PReq.pr_Window->BorderRight -1;
  604.                           y2=PReq.pr_Window->Height -PReq.pr_Window->BorderBottom-1;
  605.                           if(x2>x1 && y2>y1)
  606.                             EraseRect(PReq.pr_Window->RPort,x1,y1,x2,y2);
  607.                         }
  608.                         PReq.pr_GadControl=LayoutPaletteGads(PReq.pr_Window,pvi);
  609.                         LG_AddGadgets(PReq.pr_Window,PReq.pr_GadControl);
  610.                         UpdateSliders();
  611.                         DrawPenBox();
  612.                         break;
  613.                       case IDCMP_MOUSEMOVE:
  614.                       case IDCMP_GADGETUP:
  615.                         ProcessPalGads(imsg);
  616.                         break;
  617.                       case IDCMP_VANILLAKEY:
  618.                         ProcessPalKeys(imsg);
  619.                         break;
  620.                       case IDCMP_MENUPICK:
  621.                         ProcessPalMenus(DN,PReq.pr_MenuStrip,imsg);
  622.                         break;            
  623.                     }
  624.                     GT_ReplyIMsg(imsg);
  625.                   }
  626.                 }
  627.                 LG_FreeGadgets(PReq.pr_GadControl);
  628.                 if(PReq.pr_RetVal)
  629.                 {
  630.                   FreeVec(DN->Palette);
  631.                   
  632.                   DN->Colors=0;
  633.                   if(DN->Palette=AllocVec(sizeof(struct RGB)*PReq.pr_Colors,MEMF_CLEAR|MEMF_PUBLIC))
  634.                   {
  635.                     DN->Colors=PReq.pr_Colors;
  636.                     CopyMem(PReq.pr_Palette,DN->Palette,sizeof(struct RGB)*DN->Colors);
  637.                   }
  638.                 }
  639.               }
  640.               ClearMenuStrip(PReq.pr_Window);
  641.             }
  642.           } 
  643.           FreeMenus(PReq.pr_MenuStrip);
  644.         }
  645.         
  646.         proc->pr_WindowPtr=winptr;
  647.         CloseWindow(PReq.pr_Window);
  648.       }
  649.       FreeVisualInfo(pvi);
  650.     }
  651.     CloseScreen(pscr);
  652.   }
  653.   FreeVec(pwintitle);
  654.   
  655.   /*
  656.   if(V39)
  657.   {
  658.     while(PenCount)
  659.     {
  660.       PenCount--;
  661.       ReleasePen(cm,PenArray[PenCount]);
  662.     }
  663.   }
  664.   */
  665.   
  666.   NotBusy(Win,req);
  667. }
  668. */
  669.  
  670. void DrawPenBox(void)
  671. {
  672. //  printf("DrawPenBox %x %x %d %d %d %d\n",PReq.pr_Window->RPort, PReq.pr_Window,PReq.pr_PLeft,PReq.pr_PTop,PReq.pr_PWidth,PReq.pr_PHeight );
  673.  
  674.   DrawBevelBox(PReq.pr_Window->RPort,PReq.pr_PLeft,PReq.pr_PTop,
  675.                                      PReq.pr_PWidth,PReq.pr_PHeight ,
  676.                                      GTBB_Recessed,     1,
  677.                                      GT_VisualInfo,     PReq.pr_VI,
  678.                                      TAG_DONE);
  679.   DrawPenRect();
  680. }
  681.  
  682. void DrawPenRect(void)
  683. {
  684. //  printf("DrawPenRect %x %x %d %d %d %d\n",PReq.pr_Window->RPort,PReq.pr_Window,PReq.pr_PLeft,PReq.pr_PTop,PReq.pr_PWidth,PReq.pr_PHeight );
  685.   SetAPen (PReq.pr_Window->RPort,PReq.pr_ActiveColor);
  686.   RectFill(PReq.pr_Window->RPort, PReq.pr_PLeft+2, PReq.pr_PTop+1,
  687.                                   PReq.pr_PLeft+PReq.pr_PWidth-3,PReq.pr_PTop+PReq.pr_PHeight-2);
  688. }
  689.  
  690.  
  691.  
  692.  
  693. void ProcessPalGads(struct IntuiMessage *IMsg)
  694. {
  695.   struct Gadget *gad;
  696.   ULONG code,id;
  697.   
  698.   code=IMsg->Code;
  699.   gad=(struct Gadget *)IMsg->IAddress;
  700.   id=gad->GadgetID;
  701.   
  702.   switch(id)
  703.   {
  704.     case PGD_CANCEL:
  705.       PGD_Cancel();
  706.       break;
  707.     case PGD_OK:
  708.       PGD_Ok();
  709.       break;
  710.     case PGD_PALETTE:
  711.       PGD_PaletteModed(code,FALSE);
  712.       break;
  713.     case PGD_RED:
  714.       PGD_Red(code,FALSE);
  715.       break;
  716.     case PGD_GREEN:
  717.       PGD_Green(code,FALSE);
  718.       break;
  719.     case PGD_BLUE:
  720.       PGD_Blue(code,FALSE);
  721.       break;
  722.     case PGD_UNDO:
  723.       PGD_Undo();
  724.       break;
  725.     case PGD_RESET:
  726.       PGD_Reset();
  727.       break;
  728.     case PGD_COPY:
  729.       PGD_Copy();
  730.       break;
  731.     case PGD_SWAP:
  732.       PGD_Swap();
  733.       break;
  734.     case PGD_SPREAD:
  735.       PGD_Spread();
  736.       break;
  737.   }
  738. }
  739.  
  740.  
  741. void ProcessPalKeys(struct IntuiMessage *Imsg)
  742. {
  743. //  struct LG_RetVal lgrv;
  744.   WORD direction=1;
  745.   ULONG gadid,code;
  746.   
  747.   /*
  748.   lgrv=MatchKey2Gad(Imsg->Code,PReqLetters);
  749.   
  750.   if(lgrv.Char & 1) direction=-1;
  751. */
  752.  
  753.   if(LG_GadForKey(PReq.pr_GadControl,Imsg->Code,&gadid,&code))  
  754.   {
  755.     if(code&1) direction=-1;
  756.     
  757.     switch(gadid)
  758.     {
  759.       case PGD_PALETTE:
  760.         PGD_Palette(PReq.pr_ActiveColor+direction,TRUE);
  761.         break;
  762.       case PGD_RED:
  763.         PGD_Red(PReq.pr_RedLevel+direction,TRUE);
  764.         break;
  765.       case PGD_GREEN:
  766.         PGD_Green(PReq.pr_GreenLevel+direction,TRUE);
  767.         break;
  768.       case PGD_BLUE:
  769.         PGD_Blue(PReq.pr_BlueLevel+direction,TRUE);
  770.         break;
  771.       case PGD_RESET:
  772.         PGD_Reset();
  773.         break;
  774.       case PGD_UNDO:
  775.         PGD_Undo();
  776.         break;
  777.       case PGD_COPY:
  778.         if(PGD_Copy())
  779.           PGD_PaletteModed(PReq.pr_ActiveColor,TRUE);
  780.         break;
  781.       case PGD_SWAP:
  782.         if(PGD_Swap())
  783.           PGD_PaletteModed(PReq.pr_ActiveColor,TRUE);
  784.         break;
  785.       case PGD_SPREAD:
  786.         if(PGD_Spread())
  787.           PGD_PaletteModed(PReq.pr_ActiveColor,TRUE);
  788.         break;
  789.       case PGD_OK:
  790.         PGD_Ok();
  791.         break;
  792.       case PGD_CANCEL:
  793.         PGD_Cancel();
  794.         break;
  795.     }
  796.   }
  797. }
  798.  
  799. void PGD_Cancel(void)
  800. {
  801.   PalGo=FALSE;
  802.   PReq.pr_RetVal=0;
  803. }
  804.  
  805. void PGD_Ok(void)
  806. {
  807.   PalGo=FALSE;
  808.   PReq.pr_RetVal=1;
  809. }
  810.  
  811. void PGD_Palette(LONG Active, BOOL Keyed)
  812. {
  813.   if(Active<0)
  814.     Active=PReq.pr_Colors-1;
  815.   else
  816.     if(Active>=PReq.pr_Colors)
  817.       Active=0;
  818.   
  819.   PReq.pr_ActiveColor=Active;
  820.   if(Keyed)
  821.     LG_SetGadgetAttrs(PReq.pr_GadControl,PGD_PALETTE,
  822.                         GTPA_Color,Active,
  823.                         TAG_DONE);
  824.  
  825.  
  826.   UpdateSliders();
  827. }
  828.  
  829.  
  830. void PGD_PaletteModed(LONG Active, BOOL Keyed)
  831. {
  832.   if(Active<0)
  833.     Active=PReq.pr_Colors-1;
  834.   else
  835.     if(Active>=PReq.pr_Colors)
  836.       Active=0;
  837.       
  838.   switch(PReq.pr_Mode)
  839.   {
  840.     case PRM_COPY:
  841.       {
  842.         ULONG color;
  843.         
  844.         SetUndoBuffer();
  845.     
  846.         color=PReq.pr_ModeColor;
  847.     
  848.         PReq.pr_Palette[Active].Red    =PReq.pr_Palette[color].Red;
  849.         PReq.pr_Palette[Active].Green  =PReq.pr_Palette[color].Green;
  850.         PReq.pr_Palette[Active].Blue   =PReq.pr_Palette[color].Blue;      
  851.       }
  852.       break;
  853.     case PRM_SWAP:
  854.       {
  855.         ULONG color,dr,dg,db;
  856.     
  857.         SetUndoBuffer();
  858.     
  859.         color=PReq.pr_ModeColor;
  860.       
  861.         dr=PReq.pr_Palette[color].Red;
  862.         dg=PReq.pr_Palette[color].Green;
  863.         db=PReq.pr_Palette[color].Blue;        
  864.     
  865.         PReq.pr_Palette[color].Red    =PReq.pr_Palette[Active].Red;
  866.         PReq.pr_Palette[color].Green  =PReq.pr_Palette[Active].Green;
  867.         PReq.pr_Palette[color].Blue   =PReq.pr_Palette[Active].Blue;      
  868.   
  869.         PReq.pr_Palette[Active].Red   =dr;
  870.         PReq.pr_Palette[Active].Green =dg;
  871.         PReq.pr_Palette[Active].Blue  =db;
  872.       }
  873.       break;
  874.     case PRM_SPREAD:
  875.       {
  876.         ULONG l,color,c1,c2,c3,r1,r2,g1,g2,b1,b2;
  877.         float p;
  878.         
  879.         color=PReq.pr_ModeColor;
  880.         
  881.         SetUndoBuffer();
  882.         
  883.         if(color>Active)
  884.         {
  885.           c1=Active;
  886.           c2=color;
  887.         }
  888.         else
  889.         {
  890.           c1=color;
  891.           c2=Active;
  892.         }
  893.         
  894.         c3=c2-c1;
  895.         
  896.         if(c3>1)
  897.         {
  898.           r1=PReq.pr_Palette[c1].Red;
  899.           g1=PReq.pr_Palette[c1].Green;
  900.           b1=PReq.pr_Palette[c1].Blue;
  901.           
  902.           r2=PReq.pr_Palette[c2].Red;
  903.           g2=PReq.pr_Palette[c2].Green;
  904.           b2=PReq.pr_Palette[c2].Blue;
  905.             for(l=c1+1;l<c2;l++)
  906.           {
  907.             p=(float)(l-c1)/(float)c3;
  908.             PReq.pr_Palette[l].Red  =Mix(r1,r2,p);
  909.             PReq.pr_Palette[l].Green=Mix(g1,g2,p);
  910.             PReq.pr_Palette[l].Blue =Mix(b1,b2,p);  
  911.           }
  912.         }
  913.       }
  914.       break;
  915.   }
  916.   PReq.pr_Mode=PRM_NORMAL;
  917.   
  918.   SetPalette();
  919.   PGD_Palette(Active,Keyed);
  920. }
  921.  
  922. void PGD_Red(LONG Value, BOOL Keyed)
  923. {
  924.   ULONG bitval;
  925.  
  926.   if(Value>PReq.pr_RedMax)
  927.     Value=0;
  928.   else
  929.     if(Value<0)
  930.       Value=PReq.pr_RedMax;
  931.  
  932.   
  933.   PReq.pr_RedLevel=Value;
  934.  
  935.   if(PReq.pr_PrevColor!=PReq.pr_ActiveColor)
  936.   {
  937.     SetUndoBuffer();
  938.   }    
  939.   bitval=Value*PReq.pr_RedMult;
  940.   bitval=bitval |  bitval << 8 | bitval << 16 | bitval <<24;
  941.   PReq.pr_Palette[PReq.pr_ActiveColor].Red=bitval;
  942.   SetPalette();
  943.   if(Keyed)
  944.     UpdateSliders();
  945. }
  946.  
  947. void PGD_Green(LONG Value, BOOL Keyed)
  948. {  
  949.   ULONG bitval;
  950.  
  951.   if(Value>PReq.pr_GreenMax)
  952.     Value=0;
  953.   else
  954.     if(Value<0)
  955.       Value=PReq.pr_GreenMax;
  956.  
  957.   PReq.pr_GreenLevel=Value;
  958.  
  959.   if(PReq.pr_PrevColor!=PReq.pr_ActiveColor)
  960.   {
  961.     SetUndoBuffer();
  962.   }
  963.   bitval=Value*PReq.pr_GreenMult;
  964.   bitval=bitval |  bitval << 8 | bitval << 16 | bitval <<24;
  965.   PReq.pr_Palette[PReq.pr_ActiveColor].Green=bitval;
  966.   SetPalette();
  967.   if(Keyed)
  968.     UpdateSliders();
  969. }
  970.  
  971. void PGD_Blue(LONG Value, BOOL Keyed)
  972. {  
  973.   ULONG bitval;
  974.  
  975.   if(Value>PReq.pr_BlueMax)
  976.     Value=0;
  977.   else
  978.     if(Value<0)
  979.       Value=PReq.pr_BlueMax;
  980.  
  981.   PReq.pr_BlueLevel=Value;
  982.  
  983.   if(PReq.pr_PrevColor!=PReq.pr_ActiveColor)
  984.   {
  985.     SetUndoBuffer();
  986.   }
  987.   bitval=Value*PReq.pr_BlueMult;
  988.   bitval=bitval |  bitval << 8 | bitval << 16 | bitval <<24;
  989.   PReq.pr_Palette[PReq.pr_ActiveColor].Blue=bitval;          
  990.   SetPalette();
  991.   if(Keyed)
  992.     UpdateSliders();
  993. }
  994.  
  995. void PGD_Undo(void)
  996. {
  997.   GetUndoBuffer();
  998.   UpdateSliders();
  999.   SetPalette();
  1000. }
  1001.  
  1002. void PGD_Reset(void)
  1003. {
  1004.   GetPalette();
  1005.   UpdateSliders();
  1006.   SetPalette();
  1007. }
  1008.  
  1009. BOOL PGD_Copy(void)
  1010. {
  1011.   if( PReq.pr_Mode!=PRM_COPY)
  1012.   {
  1013.     PReq.pr_ModeColor=PReq.pr_ActiveColor;
  1014.     PReq.pr_Mode=PRM_COPY;
  1015.     return(FALSE);
  1016.   }
  1017.   else
  1018.     return(TRUE);
  1019. }
  1020.  
  1021. BOOL PGD_Swap(void)
  1022. {
  1023.   if( PReq.pr_Mode!=PRM_SWAP)
  1024.   {
  1025.     PReq.pr_ModeColor=PReq.pr_ActiveColor;
  1026.     PReq.pr_Mode=PRM_SWAP;
  1027.      return(FALSE);
  1028.   }
  1029.   else
  1030.     return(TRUE);
  1031. }
  1032. BOOL PGD_Spread(void)
  1033. {
  1034.   if( PReq.pr_Mode!=PRM_SPREAD)
  1035.   {
  1036.     PReq.pr_ModeColor=PReq.pr_ActiveColor;
  1037.     PReq.pr_Mode=PRM_SPREAD;
  1038.    return(FALSE);
  1039.   }
  1040.   else
  1041.     return(TRUE);
  1042. }
  1043.  
  1044. void SetPalette(void)
  1045. {
  1046.   if(V39)
  1047.   {
  1048.     LoadRGB32(&PReq.pr_Window->WScreen->ViewPort,(ULONG *)&PReq.pr_Colors);
  1049.   }
  1050.   else
  1051.   {
  1052.     UWORD palette[MAX_PAL_COLORS];
  1053.     UWORD l;
  1054.     
  1055.     for(l=0;l<PReq.pr_Colors;l++)
  1056.       palette[l]=((PReq.pr_Palette[l].Red   >> 20) & 0xf00) + 
  1057.                  ((PReq.pr_Palette[l].Green >> 24) & 0xf0)  + 
  1058.                  ((PReq.pr_Palette[l].Blue  >> 28) & 0xf);
  1059.     
  1060.       LoadRGB4(&PReq.pr_Window->WScreen->ViewPort,palette,PReq.pr_Colors);
  1061.   }
  1062.   DrawPenRect();  
  1063. }
  1064.  
  1065. void UpdateSliders(void)
  1066. {
  1067.   ULONG pen;
  1068.   
  1069.   pen=PReq.pr_ActiveColor;
  1070.  
  1071.   PReq.pr_RedLevel=PReq.pr_Palette[pen].Red>>(32-PReq.pr_RedBits);  
  1072.   LG_SetGadgetAttrs(PReq.pr_GadControl,PGD_RED,
  1073.                     GTSL_Level,PReq.pr_RedLevel,
  1074.                     TAG_DONE);
  1075.   
  1076.   PReq.pr_GreenLevel=PReq.pr_Palette[pen].Green>>(32-PReq.pr_GreenBits);
  1077.   LG_SetGadgetAttrs(PReq.pr_GadControl,PGD_GREEN,
  1078.                     GTSL_Level,PReq.pr_GreenLevel,
  1079.                     TAG_DONE);
  1080.  
  1081.   PReq.pr_BlueLevel=PReq.pr_Palette[pen].Blue>>(32-PReq.pr_BlueBits);
  1082.   LG_SetGadgetAttrs(PReq.pr_GadControl,PGD_BLUE,
  1083.                     GTSL_Level,PReq.pr_BlueLevel,
  1084.                     TAG_DONE);
  1085.  
  1086.   DrawPenRect();
  1087. }
  1088.  
  1089. void GetPalette(void)
  1090. {
  1091.   ULONG l;
  1092.   
  1093.   
  1094.   if(PReq.pr_DefaultNode->Colors && PReq.pr_DefaultNode->Palette)
  1095.   {
  1096.     for(l=0;l<PReq.pr_DefaultNode->Colors && l<PReq.pr_Colors;l++)
  1097.       PReq.pr_Palette[l]=PReq.pr_DefaultNode->Palette[l];
  1098.   }
  1099.   else  // Duplicate ModePro's Screen pens
  1100.   { 
  1101.     if(V39)
  1102.     {
  1103.       GetRGB32(Scr->ViewPort.ColorMap,0,(ULONG)PReq.pr_Colors,(ULONG *)PReq.pr_Palette);
  1104.     }
  1105.     else
  1106.     {
  1107.       ULONG color;
  1108.       struct ColorMap *cm;
  1109.       
  1110.       cm=Scr->ViewPort.ColorMap;
  1111.       for(l=0;l<PReq.pr_Colors;l++)
  1112.       {
  1113.         color=GetRGB4(cm,l);
  1114.         PReq.pr_Palette[l].Red  =(color & 0x00000f00) << 20;
  1115.         PReq.pr_Palette[l].Green=(color & 0xf0)       << 24;
  1116.         PReq.pr_Palette[l].Blue =(color & 0xf)        << 28;
  1117.       }
  1118.     }
  1119.   }
  1120. }
  1121.  
  1122. void SetUndoBuffer(void)
  1123. {
  1124.   ULONG colors;
  1125.   
  1126.   colors=PReq.pr_Colors;
  1127.   
  1128.   CopyMem(PReq.pr_Palette, PReq.pr_UndoBuffer, colors * sizeof(struct RGB));
  1129.   PReq.pr_PrevColor=PReq.pr_ActiveColor;
  1130. }
  1131.  
  1132. void GetUndoBuffer(void)
  1133. {
  1134.   ULONG colors;
  1135.   struct RGB dummy[MAX_PAL_COLORS];
  1136.   
  1137.   colors=min(PReq.pr_Colors,MAX_PAL_COLORS);
  1138.   
  1139.   CopyMem(PReq.pr_UndoBuffer,dummy              , colors * sizeof(struct RGB));
  1140.   CopyMem(PReq.pr_Palette   ,PReq.pr_UndoBuffer , colors * sizeof(struct RGB));
  1141.   CopyMem(dummy             ,PReq.pr_Palette    , colors * sizeof(struct RGB));
  1142. }
  1143.  
  1144. ULONG Mix(ULONG A,ULONG B, float Percent)
  1145. {
  1146.   float a,b,diff;
  1147.   
  1148.   a=A;
  1149.   b=B;
  1150.   
  1151.   diff=b-a;
  1152.  
  1153.   a+=diff*Percent;
  1154.  
  1155.   return((ULONG) (a));
  1156. }
  1157.  
  1158.  
  1159.  
  1160. void ProcessPalMenus(struct DefaultNode *DN, struct Menu *MenuStrip, struct IntuiMessage *imsg)
  1161. {
  1162.   UWORD  menunumber,ns;
  1163.   ULONG  menuid;
  1164.   struct MenuItem *mi;
  1165.   struct RGB palette[MAX_PAL_COLORS];
  1166.   
  1167.   menunumber=imsg->Code;
  1168.   mi=ItemAddress(MenuStrip,menunumber);
  1169.   if(mi)
  1170.   do
  1171.   {
  1172.     menuid=(ULONG)(GTMENUITEM_USERDATA(mi));
  1173.     switch(menuid)
  1174.     {
  1175.       case M_PALETTELOAD:
  1176.         if(SelectPaletteFile(GetString(MSG_PRM_LOADPALETTE),0,PReq.pr_Window))
  1177.         {
  1178.           CopyMem(PReq.pr_Palette,palette,PReq.pr_Colors*sizeof(struct RGB));
  1179.           if(ReadCMAP(filename,palette,PReq.pr_Colors))
  1180.           {
  1181.             SetUndoBuffer();
  1182.             CopyMem(palette,PReq.pr_Palette,PReq.pr_Colors*sizeof(struct RGB));
  1183.             SetPalette();
  1184.             UpdateSliders();
  1185.           }
  1186.         }
  1187.         break;
  1188.         
  1189.       case M_PALETTESAVE:
  1190.         if(SelectPaletteFile(GetString(MSG_PRM_SAVEPALETTE),1,PReq.pr_Window))
  1191.         {
  1192.           CopyMem(PReq.pr_Palette,palette,PReq.pr_Colors*sizeof(struct RGB));
  1193.           if(!SaveCMAP(filename,palette,PReq.pr_Colors))
  1194.           {
  1195.             LONG error;
  1196.             UBYTE errstr[91];
  1197.  
  1198.             if(error=IoErr())
  1199.             {
  1200.               Fault(error,MiscText[SAVE_ERROR],errstr,90);
  1201.               EZReq(PReq.pr_Window,0,ModePro,errstr,MiscText[OK],0);
  1202.             }
  1203.           }
  1204.         }
  1205.         break;
  1206.         
  1207.       case M_PALETTEOK:
  1208.         PalGo=FALSE;
  1209.         PReq.pr_RetVal=1;
  1210.         break;
  1211.         
  1212.       case M_PALETTECANCEL:
  1213.         PalGo=FALSE;
  1214.         PReq.pr_RetVal=0;
  1215.         break;
  1216.       
  1217.     }
  1218.     ns=mi->NextSelect;
  1219.     mi=ItemAddress(MenuStrip,mi->NextSelect);
  1220.   }while(ns!= MENUNULL && mi);
  1221. }
  1222.  
  1223.  
  1224. BOOL SelectPaletteFile(UBYTE *title,UBYTE save,struct Window *Parent)
  1225.   WORD width;
  1226.   
  1227.   if(!PalFileReq)
  1228.   {
  1229.     width=Parent->Width/2;
  1230.     PalFileReq=(struct FileRequester *)AllocAslRequestTags(ASL_FileRequest, 
  1231.                     ASLFR_TextAttr,        TAttr,
  1232.                     ASLFR_InitialTopEdge,  Parent->TopEdge+8,
  1233.                     ASLFR_InitialLeftEdge, Parent->LeftEdge+width/2,
  1234.                     ASLFR_InitialWidth,    width,
  1235.                     ASLFR_InitialHeight,   Parent->Height-16,
  1236.                     ASLFR_DoPatterns  ,    TRUE,
  1237.                     ASLFR_InitialPattern,  "#?",
  1238.                     ASLFR_RejectIcons,     TRUE,
  1239.                     ASLFR_PrivateIDCMP,    TRUE,
  1240.                     TAG_DONE); 
  1241.   }
  1242.   if(PalFileReq)
  1243.   {
  1244.     if(AslRequestTags(PalFileReq, 
  1245.                   ASLFR_Window   ,       Parent,
  1246.                   ASLFR_SleepWindow ,    TRUE,
  1247.                   ASLFR_TitleText,       title,
  1248.                   ASLFR_DoSaveMode,      save,
  1249.                   TAG_DONE))
  1250.     {
  1251.       strncpy(filename,PalFileReq->fr_Drawer,512);
  1252.       if(AddPart(filename,PalFileReq->fr_File,512))
  1253.         return(TRUE);
  1254.     }   
  1255.   }
  1256.   return(FALSE);
  1257. }
  1258.  
  1259.  
  1260. /*
  1261. BOOL NewPaletteReq()
  1262.     /*
  1263.     if(drawinfo=GetScreenDrawInfo(pscr))
  1264.     {
  1265.       //PaletteWinHook.h_Data=drawinfo->dri_Pens[BACKGROUNDPEN];
  1266.       FreeScreenDrawInfo(pscr,drawinfo);
  1267.     }
  1268. */
  1269.     PReq.pr_Colors=1<<pscr->RastPort.BitMap->Depth;
  1270.     PReq.pr_Colors=min(PReq.pr_Colors,MAX_PAL_COLORS);
  1271.     PReq.pr_Palette[PReq.pr_Colors].Red=0;
  1272.     PReq.pr_ActiveColor=0;
  1273.     PReq.pr_DefaultNode=DN;
  1274.     PReq.pr_Mode=0;
  1275.   
  1276.     if(V39)
  1277.       PReq.pr_RedBits   = PReq.pr_GreenBits = PReq.pr_BlueBits = 8;
  1278.     else
  1279.       PReq.pr_RedBits   = PReq.pr_GreenBits = PReq.pr_BlueBits = 4;
  1280.       
  1281.     GetPalette();
  1282.   
  1283.     SetUndoBuffer();
  1284.     
  1285.     PReq.pr_PrevColor=PReq.pr_ActiveColor;
  1286.     
  1287.     if(pvi=GetVisualInfo(pscr,0))
  1288.     {
  1289.       PReq.pr_VI=pvi;
  1290.       if(PReq.pr_Window=OpenWindowTags(0,
  1291.                         WA_CustomScreen,pscr,
  1292.                         WA_Width      ,pscr->Width,
  1293.                         WA_Height     ,pscr->Height-pscr->BarHeight-1,
  1294.                         WA_Left       ,0,
  1295.                         WA_Top        ,pscr->BarHeight+1,
  1296.                         WA_Title      ,pwintitle,
  1297.                         WA_MaxWidth   ,-1,
  1298.                         WA_MaxHeight  ,-1,
  1299.                         WA_MinWidth   ,150,
  1300.                         WA_MinHeight  ,150,
  1301.                         WA_IDCMP      ,PALETTEIDCMP | SLIDERIDCMP | BUTTONIDCMP | 
  1302.                                        IDCMP_CLOSEWINDOW|IDCMP_NEWSIZE|IDCMP_VANILLAKEY|IDCMP_MENUPICK|IDCMP_REFRESHWINDOW,
  1303.                         WA_SizeGadget ,TRUE,
  1304.                         WA_DragBar    ,TRUE,
  1305.                         WA_DepthGadget,TRUE,
  1306.                         WA_CloseGadget,TRUE,
  1307.                         WA_SizeBBottom,TRUE,
  1308.                         WA_Activate   ,TRUE,
  1309.                         WA_NewLookMenus,TRUE,
  1310.                         WA_SimpleRefresh    ,TRUE,
  1311. //                        WA_BackFill   ,&PaletteWinHook,
  1312.                         TAG_DONE))
  1313.       {
  1314.         struct Process *proc;
  1315.         struct Window *winptr;
  1316.     
  1317.         proc=(struct Process *)FindTask(0);
  1318.         winptr=proc->pr_WindowPtr;
  1319.         proc->pr_WindowPtr=PReq.pr_Window;
  1320.         
  1321.         if(PReq.pr_MenuStrip=CreateMenus(PaletteMenu,TAG_END))
  1322.         {
  1323.           if(LayoutMenus(PReq.pr_MenuStrip,pvi,
  1324.                           GTMN_NewLookMenus,TRUE,
  1325.                           TAG_END))
  1326.           {
  1327.             if(SetMenuStrip(PReq.pr_Window,PReq.pr_MenuStrip))
  1328.             {
  1329.               SetPalette();
  1330.               
  1331.               if(GetDisplayInfoData(0,(UBYTE *)&di,sizeof(di),DTAG_DISP,DN->ModeID))
  1332.               {
  1333.                 if(V39)
  1334.                 {
  1335.                   PReq.pr_RedBits   =max(di.RedBits,1);
  1336.                   PReq.pr_GreenBits =max(di.GreenBits,1);
  1337.                   PReq.pr_BlueBits  =max(di.BlueBits,1);
  1338.                 }
  1339.                 else
  1340.                 {
  1341.                   /** This needs to be fixed for V36 **/
  1342.                   PReq.pr_RedBits   = PReq.pr_GreenBits = PReq.pr_BlueBits = 4;
  1343.                   /**********************************************************
  1344.                   PReq.pr_RedBits   = sqrt(((di.PaletteRange & 0xf00) >> 8)+1);
  1345.                   PReq.pr_GreenBits = sqrt(((di.PaletteRange & 0xf0) >> 4)+1);
  1346.                   PReq.pr_BlueBits  = sqrt(((di.PaletteRange & 0xf )  +1));
  1347.                   PReq.pr_RedBits   = max(PReq.pr_RedBits      ,1);
  1348.                   PReq.pr_GreenBits = max(PReq.pr_GreenBits    ,1);
  1349.                   PReq.pr_BlueBits  = max(PReq.pr_BlueBits     ,1);
  1350.                   */
  1351.                 }
  1352.                 
  1353.               }
  1354.               
  1355.               PReq.pr_RedMax   =(1<<PReq.pr_RedBits  )-1;
  1356.               PReq.pr_GreenMax =(1<<PReq.pr_GreenBits)-1;
  1357.               PReq.pr_BlueMax  =(1<<PReq.pr_BlueBits )-1;
  1358.               
  1359.               PReq.pr_RedMult   =255/PReq.pr_RedMax;
  1360.               PReq.pr_GreenMult =255/PReq.pr_GreenMax;
  1361.               PReq.pr_BlueMult  =255/PReq.pr_BlueMax;
  1362.               
  1363.       
  1364.               //printf("%8x %8x\n",PReq.pr_Window->RPort,GUIFont);
  1365.               SetFont(PReq.pr_Window->RPort,GUIFont);
  1366.               
  1367.               //printf("setfont\n");
  1368.               
  1369.               if(PReq.pr_GadControl=LayoutPaletteGads(PReq.pr_Window,pvi))
  1370.               {
  1371.                 //printf("Layout\n");
  1372.                 DrawPenBox();
  1373.                 UpdateSliders();
  1374.                 WindowLimits(PReq.pr_Window,MinWidth,MinHeight,-1,-1);
  1375.                 LG_AddGadgets(PReq.pr_Window,PReq.pr_GadControl);
  1376.                 while(PalGo)
  1377.                 {
  1378.                   WaitPort(PReq.pr_Window->UserPort);
  1379.                   while(imsg=GT_GetIMsg(PReq.pr_Window->UserPort))
  1380.                   {
  1381.                     switch(imsg->Class)
  1382.                     {
  1383.                       case IDCMP_REFRESHWINDOW:
  1384.                         GT_BeginRefresh(PReq.pr_Window);
  1385.                         DrawPenBox();
  1386.                         GT_EndRefresh(PReq.pr_Window,TRUE);
  1387.                         break;
  1388.                         
  1389.                       case IDCMP_CLOSEWINDOW:
  1390.                         PReq.pr_RetVal=FALSE;
  1391.                         PalGo=FALSE;
  1392.                         break;
  1393.                         
  1394.                       case IDCMP_NEWSIZE:
  1395.                         LG_FreeGadgets(PReq.pr_GadControl);
  1396.                         RefreshWindowFrame(PReq.pr_Window);
  1397.                         {
  1398.                           WORD x1,y1,x2,y2;
  1399.                           
  1400.                           x1=PReq.pr_Window->BorderLeft;
  1401.                           y1=PReq.pr_Window->BorderTop;
  1402.                           x2=PReq.pr_Window->Width  -PReq.pr_Window->BorderRight -1;
  1403.                           y2=PReq.pr_Window->Height -PReq.pr_Window->BorderBottom-1;
  1404.                           if(x2>x1 && y2>y1)
  1405.                             EraseRect(PReq.pr_Window->RPort,x1,y1,x2,y2);
  1406.                         }
  1407.                         PReq.pr_GadControl=LayoutPaletteGads(PReq.pr_Window,pvi);
  1408.                         LG_AddGadgets(PReq.pr_Window,PReq.pr_GadControl);
  1409.                         UpdateSliders();
  1410.                         DrawPenBox();
  1411.                         break;
  1412.                       case IDCMP_MOUSEMOVE:
  1413.                       case IDCMP_GADGETUP:
  1414.                         ProcessPalGads(imsg);
  1415.                         break;
  1416.                       case IDCMP_VANILLAKEY:
  1417.                         ProcessPalKeys(imsg);
  1418.                         break;
  1419.                       case IDCMP_MENUPICK:
  1420.                         ProcessPalMenus(DN,PReq.pr_MenuStrip,imsg);
  1421.                         break;            
  1422.                     }
  1423.                     GT_ReplyIMsg(imsg);
  1424.                   }
  1425.                 }
  1426.                 LG_FreeGadgets(PReq.pr_GadControl);
  1427.                 if(PReq.pr_RetVal)
  1428.                 {
  1429.                   FreeVec(DN->Palette);
  1430.                   
  1431.                   DN->Colors=0;
  1432.                   if(DN->Palette=AllocVec(sizeof(struct RGB)*PReq.pr_Colors,MEMF_CLEAR|MEMF_PUBLIC))
  1433.                   {
  1434.                     DN->Colors=PReq.pr_Colors;
  1435.                     CopyMem(PReq.pr_Palette,DN->Palette,sizeof(struct RGB)*DN->Colors);
  1436.                   }
  1437.                 }
  1438.               }
  1439.               ClearMenuStrip(PReq.pr_Window);
  1440.             }
  1441.           } 
  1442.           FreeMenus(PReq.pr_MenuStrip);
  1443.         }
  1444.         
  1445.         proc->pr_WindowPtr=winptr;
  1446.         CloseWindow(PReq.pr_Window);
  1447.       }
  1448.       FreeVisualInfo(pvi);
  1449.     }
  1450. */
  1451. */
  1452.  
  1453. void PaletteReq(struct DefaultNode *DN)
  1454. {
  1455.   struct DisplayInfo di;
  1456.   struct DimensionInfo dim;
  1457.   struct Screen *pscr;
  1458.   APTR pvi;
  1459.   struct IntuiMessage *imsg;
  1460.   STRPTR pwintitle,pwinfmt;
  1461.   LONG pwinfmtlen;
  1462.   WORD dripens[DRIPENS+1];
  1463.   WORD swidth=320,sheight=200;
  1464.   LONG l;
  1465.  
  1466.  
  1467.   pwinfmt=GetString(MSG_REQ_EDIT_PALETTE);
  1468.   pwinfmtlen=strlen(pwinfmt);
  1469.   if(pwintitle=AllocVec(pwinfmtlen+strlen(DN->Def_Node.ln_Name)+1,MEMF_PUBLIC|MEMF_CLEAR))
  1470.     sprintf(pwintitle,pwinfmt,DN->Def_Node.ln_Name);
  1471.   
  1472.   if(DN->Look3D)
  1473.   {
  1474.     for(l=0;l<DRIPENS;l++)
  1475.       dripens[l]=DN->Pens[l];
  1476.     dripens[DRIPENS]=~0;
  1477.   }
  1478.   else
  1479.     dripens[0]=~0;
  1480.  
  1481.   req=Busy(Win);  
  1482.  
  1483.   if(GetDisplayInfoData(0,(UBYTE *)&dim,sizeof(dim),DTAG_DIMS,DN->ModeID))
  1484.   {
  1485.     swidth  =max(swidth ,dim.Nominal.MaxX);
  1486.     sheight =max(sheight,dim.Nominal.MaxY);
  1487.   }
  1488.   
  1489.   if(pscr=OpenScreenTags(0,SA_Depth     ,DN->Depth,
  1490.                            SA_DisplayID ,DN->ModeID,
  1491.                            SA_Pens      ,dripens,
  1492.                            SA_Overscan  ,OSCAN_TEXT,
  1493.                            SA_Width     ,swidth,
  1494.                            SA_Height    ,sheight,
  1495.                            SA_Title     ,pwintitle,
  1496.                            SA_AutoScroll,TRUE,
  1497.                            SA_ModeProNode,DN,
  1498.                            TAG_DONE))
  1499.   {
  1500.     LONG colors;
  1501.     
  1502.     colors=1<<(pscr->RastPort.BitMap->Depth);
  1503.     
  1504.     if(!PalettePReq)
  1505.     {
  1506.       LONG width;
  1507.       Screen
  1508.       width=Win->Width/2;
  1509.       PalettePReq=PR_AllocPaletteRequest(
  1510. //                  PR_TextAttr,        TAttr,
  1511.                   TAG_DONE);
  1512.     }
  1513.     
  1514.     if(PalettePReq)
  1515.     {
  1516.       struct RGB *newpalette;
  1517.       
  1518.       if(newpalette=AllocVec(sizeof(struct RGB)*colors,MEMF_CLEAR|MEMF_PUBLIC))
  1519.       {
  1520.         CopyMem(DN->Palette,newpalette,sizeof(struct RGB)*DN->Colors);
  1521.         if(PR_DoPaletteRequest(PalettePReq, 
  1522.                           PR_Screen,              pscr,
  1523.                           PR_Colors,              colors,
  1524.                           PR_InitialLeftEdge,     0,
  1525.                           PR_InitialTopEdge,      pscr->BarHeight+1,
  1526.                           PR_InitialWidth,        pscr->Width,,
  1527.                           PR_InitialHeight,       pscr->Height-pscr->BarHeight-1,
  1528.                           PR_InitialPalette,      newpalette,
  1529.                           TAG_DONE))
  1530.         {
  1531.           APTR dummy;
  1532.           
  1533.           dummy=DN->Palette;
  1534.           DN->Palette=newpalette;
  1535.           newpalette=dummy;
  1536.  
  1537.           DN->Colors=colors;
  1538.           CopyMem(PalettePReq.pr_Palette,DN->Palette,sizeof(struct RGB)*DN->Colors);          
  1539.         }
  1540.         FreeVec(newpalette);
  1541.       
  1542.       }
  1543.     }
  1544.     CloseScreen(pscr);
  1545.   }
  1546.   FreeVec(pwintitle);
  1547.   
  1548.   NotBusy(Win,req);
  1549. }
  1550.  
  1551.